package com.qingxin.cms.service;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qingxin.cms.entity.CmsArticle;
import com.qingxin.cms.entity.CmsCategory;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.MyUploadFileParam;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
@Service
public class CmsArticleService extends BaseService<CmsArticle, String>{
	
	@Autowired
	private CmsArticleAttachmentService cmsArticleAttachmentService;
	
	
	/**
	 * 获取文章分页数据
	 * @param pageParam
	 * @param search
	 * @param categoryId
	 * @param articleTitle
	 * @param keyWord
	 * @param articleDesc
	 * @param articleContent
	 * @param createBy
	 * @param createTime
	 * @param updateBy
	 * @param updateTime
	 * @param request
	 * @return
	 */
	public Pager list(PageParam pageParam,String search,String categoryId, HttpServletRequest request){
		Querier querier = Querier.create("select new com.qingxin.cms.entity.CmsArticle(" +
				"a.articleId, a.articleTitle, a.keyWord,a.createBy,a.createTime,a.updateBy,a.updateTime,c.categoryName)" +
				"from CmsArticle a, CmsCategory c where a.cmsCategory.categoryId = c.categoryId and a.cmsCategory.categoryId=:categoryId");
		querier.setParam("categoryId", categoryId);
		if(StringUtils.isNotEmpty(search)){
			search = "%"+search.replace("'", "")+"%";
			querier.append(" and (a.articleTitle like :articleTitle)");
			querier.setParam("articleTitle", search);
		}
		querier.append(pageParam.getOrderby(" order by a.createTime desc","a"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	/**
	 * 前台列表页数据
	 * @param categoryCode
	 * @param pageParam
	 * @param request
	 * @return
	 */
	public Pager list(String categoryCode, PageParam pageParam, HttpServletRequest request){
		Querier querier = Querier.create("select new com.qingxin.cms.entity.CmsArticle(" +
				"a.articleId, a.articleTitle, a.keyWord,a.articleDesc,a.thumb,a.articleContent,a.createBy,a.createTime,a.updateBy,a.updateTime,c.categoryName)" +
				"from CmsArticle a, CmsCategory c where a.cmsCategory.categoryId = c.categoryId and c.categoryCode=:categoryCode");
		querier.setParam("categoryCode", categoryCode);
		querier.append(pageParam.getOrderby(" order by a.createTime desc","a"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	
	/**
	 * 首页文章数据
	 * @param categoryName
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<CmsArticle> list(String categoryCode,int size){
		Querier querier = Querier.create("select new com.qingxin.cms.entity.CmsArticle(" +
				"a.articleId, a.articleTitle, a.keyWord,a.articleDesc,a.thumb,a.articleContent,a.createBy,a.createTime,a.updateBy,a.updateTime,c.categoryName)" +
				"from CmsArticle a, CmsCategory c where a.cmsCategory.categoryId = c.categoryId and c.categoryCode=:categoryCode");
		querier.setParam("categoryCode", categoryCode);
		querier.append(" order by a.createTime desc");
		querier.setMaxResults(size);
		List<CmsArticle> articles = listByQuerier(querier);
		return articles;
	}
	

	/**
	 * @param categoryName
	 * @return
	 */
	public List<CmsArticle> listByPark(String categoryCode, int size, String parkid){
		Querier querier = Querier.create("select new com.qingxin.cms.entity.CmsArticle(" +
				"a.articleId, a.articleTitle, a.keyWord,a.articleDesc,a.thumb,a.articleContent,a.createBy,a.createTime,a.updateBy,a.updateTime,c.categoryName)" +
				" from CmsArticle a, CmsCategory c where a.cmsCategory.categoryId = c.categoryId and a.cmsCategory.categoryCode=:categoryCode" +
				" and a.parkid=:parkid");
		querier.setParam("categoryCode", categoryCode);
		querier.setParam("parkid", parkid);
		querier.append(" order by a.createTime desc");
		querier.setMaxResults(size);
		@SuppressWarnings("unchecked")
		List<CmsArticle> articles = listByQuerier(querier);
		return articles;
	}
	
	
	public Pager listByPark(PageParam pageParam, String categoryCode, String parkid, HttpServletRequest request){
		Querier querier = Querier.create("select new com.qingxin.cms.entity.CmsArticle(" +
				"a.articleId, a.articleTitle, a.keyWord,a.articleDesc,a.thumb,a.articleContent,a.createBy,a.createTime,a.updateBy,a.updateTime,c.categoryName)" +
				" from CmsArticle a, CmsCategory c where a.cmsCategory.categoryId = c.categoryId and a.cmsCategory.categoryCode=:categoryCode" +
				" and a.parkid=:parkid");
		querier.setParam("categoryCode", categoryCode);
		querier.setParam("parkid", parkid);
		querier.append(" order by a.createTime desc");
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	/**
	 * @param categoryName
	 * @return
	 */
	public List<CmsArticle> list(String categoryCode){
		Querier querier = Querier.create("select new com.qingxin.cms.entity.CmsArticle(" +
				"a.articleId, a.articleTitle, a.keyWord,a.articleDesc,a.thumb,a.articleContent,a.createBy,a.createTime,a.updateBy,a.updateTime,c.categoryName)" +
				"from CmsArticle a, CmsCategory c where a.cmsCategory.categoryId = c.categoryId and a.cmsCategory.categoryCode=:categoryCode");
		querier.setParam("categoryCode", categoryCode);
		querier.append(" order by a.createTime desc");
		@SuppressWarnings("unchecked")
		List<CmsArticle> articles = listByQuerier(querier);
		return articles;
	}
	
	
	/**
	 * 通过编号获取分页数据
	 * @param categoryName
	 * @return
	 */
	public Pager list(PageParam pageParam,String categoryCode,HttpServletRequest request){
		Querier querier = Querier.create("select new com.qingxin.cms.entity.CmsArticle(" +
				"a.articleId, a.articleTitle,a.thumb,a.createTime)" +
				"from CmsArticle a, CmsCategory c where a.cmsCategory.categoryId = c.categoryId and a.cmsCategory.categoryCode=:categoryCode");
		querier.setParam("categoryCode", categoryCode);
		querier.append(pageParam.getOrderby(" order by a.createTime desc"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	/**
	 * 获取轮播文章信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<CmsArticle> listCarousel(){
		List<CmsArticle> list = listByHql("from CmsArticle a where a.carousel=1 order by a.createTime desc");
		if(list != null){
			for(CmsArticle article : list){
				article.getCmsCategory().getCategoryCode();
			}
		}
		return list;
	}
	
	
	/**
	 * 添加文章
	 * @param article
	 * @param result
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(CmsArticle article, String articleTypeId, MyUploadFileParam uploadFileParam, LoginUser loginUser, HttpServletRequest request){
		Date now = new Date();
		article.setCreateTime(now);
		article.setUpdateTime(now);
		article.setCreateBy(loginUser.getLogin_name());
		article.setUpdateBy(loginUser.getLogin_name());
		if(StringUtils.isNotEmpty(article.getThumb())){
			UploadFile.moveTmpTo(article.getThumb(),UploadFile.DIR_NEWS_IMG);
		}
		article.setCmsCategory(new CmsCategory(articleTypeId));
		this.save(article);
		//添加附件 
		cmsArticleAttachmentService.attachmentDo(article.getArticleId(), uploadFileParam, loginUser);
		
		return null;
	}
	
	/**
	 * 修改文章信息
	 * @param article
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(CmsArticle article, MyUploadFileParam uploadFileParam, LoginUser loginUser, HttpServletRequest request){
		CmsArticle old = getSafeEntity(article.getArticleId());
		
		if(StringUtils.isNotEmpty(article.getThumb()) && !article.getThumb().equals(old.getThumb())){
			UploadFile.moveTmpTo(article.getThumb(),UploadFile.DIR_NEWS_IMG);
		}
		
		String fields = "articleTitle,keyWord,thumb,articleDesc,articleContent,carousel,parkid";
		BeanUtil.setTargetFields(old, article, fields, true);
		old.setUpdateBy(loginUser.getLogin_name());
		old.setUpdateTime(new Date());
		Updater<CmsArticle> updater = new Updater<>(old, fields+",updateBy,updateTime");
		this.update(updater);
		cmsArticleAttachmentService.attachmentDo(article.getArticleId(), uploadFileParam, loginUser);
		
		return null;
	}
	
	/**
	 * 删除文章信息
	 * @param articleId
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError delete(String[] articleId, HttpServletRequest request){
		for(String id : articleId){
			CmsArticle article = this.get(id);
			if(article == null){
				continue;
			}
			this.delete(article);
		}
		return null;
	}
	
}
